package com.syni.mdd.yhd.common.all.quartz.groupMessage;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessGroupMessage;
import com.syni.mdd.yhd.common.all.quartz.QuartzDo;
import com.syni.mdd.yhd.common.all.service.BmsBusinessGroupMessageService;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import org.quartz.*;
import org.quartz.impl.matchers.KeyMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @className GroupMessageTimingInit
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/8 18:16
 * @VERSION 1.0
 **/
@Component
@DependsOn("applicationContextHolder")
public class GroupMessageTimingInit {
    @Autowired
    BmsBusinessGroupMessageService bmsBusinessGroupMessageService;


    @PostConstruct
    public void init(){
        Integer pageNum = 1;
        Integer pageSize = 100;
        while (true) {
            Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize);
            Page<BmsBusinessGroupMessage> bmsBusinessGroupMessagePage = bmsBusinessGroupMessageService.getBaseDao().findAll(new Specification<BmsBusinessGroupMessage>() {
                @Override
                public Predicate toPredicate(Root<BmsBusinessGroupMessage> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                    List<Predicate> predicates = new ArrayList<>();
                    predicates.add(criteriaBuilder.isNotNull(root.get("regularlySend")));
                    predicates.add(criteriaBuilder.equal(root.get("status"), Constants.COMMON_FLAG_FALSE_INT));
                    criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
                    return null;
                }
            },pageable);
            List<BmsBusinessGroupMessage> bmsBusinessGroupMessageList = bmsBusinessGroupMessagePage.getContent();
            if(bmsBusinessGroupMessageList != null && bmsBusinessGroupMessageList.size() > 0){
                System.out.println(bmsBusinessGroupMessageList);
                for(BmsBusinessGroupMessage bmsBusinessGroupMessage : bmsBusinessGroupMessageList){
                    List<String> sendUsers = Arrays.asList(bmsBusinessGroupMessage.getReceUser().split("_"));
                    Date startDate = DateTimeUtils.converStringToDate(bmsBusinessGroupMessage.getRegularlySend(),DateTimeUtils.DATE_PATTERN_TIMESTAMP_02);
                    bmsBusinessGroupMessageService.doGroupMessageTiming(GroupMessageTimingJob.class,startDate,bmsBusinessGroupMessage,sendUsers);
                }
                pageNum++;
            }else {
                break;
            }

        }

    }
}
